package androidx.appcompat.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import b.f.d.b;
import java.util.Calendar;

class j {

    /* renamed from: d  reason: collision with root package name */
    private static j f157d;

    /* renamed from: a  reason: collision with root package name */
    private final Context f158a;

    /* renamed from: b  reason: collision with root package name */
    private final LocationManager f159b;

    /* renamed from: c  reason: collision with root package name */
    private final a f160c = new a();

    private static class a {

        /* renamed from: a  reason: collision with root package name */
        boolean f161a;

        /* renamed from: b  reason: collision with root package name */
        long f162b;

        /* renamed from: c  reason: collision with root package name */
        long f163c;

        /* renamed from: d  reason: collision with root package name */
        long f164d;
        long e;
        long f;

        a() {
        }
    }

    j(Context context, LocationManager locationManager) {
        this.f158a = context;
        this.f159b = locationManager;
    }

    static j a(Context context) {
        if (f157d == null) {
            Context applicationContext = context.getApplicationContext();
            f157d = new j(applicationContext, (LocationManager) applicationContext.getSystemService("location"));
        }
        return f157d;
    }

    @SuppressLint({"MissingPermission"})
    private Location b() {
        Location location = null;
        Location c2 = b.b(this.f158a, "android.permission.ACCESS_COARSE_LOCATION") == 0 ? c("network") : null;
        if (b.b(this.f158a, "android.permission.ACCESS_FINE_LOCATION") == 0) {
            location = c("gps");
        }
        return (location == null || c2 == null) ? location != null ? location : c2 : location.getTime() > c2.getTime() ? location : c2;
    }

    private Location c(String str) {
        try {
            if (this.f159b.isProviderEnabled(str)) {
                return this.f159b.getLastKnownLocation(str);
            }
            return null;
        } catch (Exception unused) {
            return null;
        }
    }

    private boolean e() {
        return this.f160c.f > System.currentTimeMillis();
    }

    private void f(Location location) {
        long j;
        a aVar = this.f160c;
        long currentTimeMillis = System.currentTimeMillis();
        i b2 = i.b();
        i iVar = b2;
        iVar.a(currentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
        long j2 = b2.f154a;
        iVar.a(currentTimeMillis, location.getLatitude(), location.getLongitude());
        boolean z = b2.f156c == 1;
        long j3 = b2.f155b;
        long j4 = j2;
        long j5 = b2.f154a;
        long j6 = j3;
        boolean z2 = z;
        b2.a(86400000 + currentTimeMillis, location.getLatitude(), location.getLongitude());
        long j7 = b2.f155b;
        if (j6 == -1 || j5 == -1) {
            j = 43200000 + currentTimeMillis;
        } else {
            j = (currentTimeMillis > j5 ? 0 + j7 : currentTimeMillis > j6 ? 0 + j5 : 0 + j6) + 60000;
        }
        aVar.f161a = z2;
        aVar.f162b = j4;
        aVar.f163c = j6;
        aVar.f164d = j5;
        aVar.e = j7;
        aVar.f = j;
    }

    /* access modifiers changed from: package-private */
    public boolean d() {
        a aVar = this.f160c;
        if (e()) {
            return aVar.f161a;
        }
        Location b2 = b();
        if (b2 != null) {
            f(b2);
            return aVar.f161a;
        }
        Log.i("TwilightManager", "Could not get last known location. This is probably because the app does not have any location permissions. Falling back to hardcoded sunrise/sunset values.");
        int i = Calendar.getInstance().get(11);
        return i < 6 || i >= 22;
    }
}
